<template>
    <div class="AddUser">
        <mt-header fixed title="添加管理员">
            <mt-button icon="back" @click="backprev" slot="left"></mt-button>
        </mt-header>
        <mt-tab-container style="margin-top: 40px;" >
            <mt-tab-container-item>
                <mt-field label="手机号" placeholder="请输入手机号" type="tel" v-model="mobile"></mt-field>
                <mt-field label="姓名" placeholder="请输入姓名" type="text" v-model="name"></mt-field>
                <mt-field label="昵称" placeholder="请输入昵称" type="text" v-model="userName"></mt-field>
                <mt-field label="职位" placeholder="请输入职位" type="text" v-model="position"></mt-field>
                <mt-field label="密码" placeholder="请输入密码" type="password" v-model="password"></mt-field>
                <mt-button type="primary" size="large" style="width: 90%; margin: 10px auto" @click="addSubmit">添加</mt-button>
            </mt-tab-container-item>
        </mt-tab-container>
    </div>
</template>

<script>
    import {
            loading,
            back,
            addopsuser,
            operopsuser,
            opsuserlist
    } from '../common/api/api';
    import { Toast ,Indicator} from 'mint-ui';
    export default {
        name: 'AddUser',
        data () {
            return {
                mobile : null,
                name : null,
                userName : null,
                position : null,
                password : null,
                createDialog: {
                    opened: false,
                    loading: false
                },

            }
        },
        components: {
            //Hello
        },
        methods: {
            back : back,
            backprev : function () {
                this.$router.push('/management');
            },
            addSubmit : function () {
                var vm = this;
                const params = {
                    mobile : vm.mobile,
                    name : vm.name,
                    userName : vm.userName,
                    position : vm.position,
                    password : vm.password
                }
                console.log(params);
                if(!vm.checkParams(params)){
                    return false;
                }
                loading(true);
                addopsuser(params, {
                    target: vm.createDialog
                })
                        .then((result) => {
                    console.log(result);
                vm.createDialog.opened = false;
                if(result.errno == 0){
                    loading(false);
                    Toast(result.errmsg);
                    this.$router.push('/management');

                }else{
                    loading(false);
                    Toast(result.errmsg);
                }

            });
            },
            checkParams : function (params) {
                if(params.mobile == null || params.mobile == ""){
                    Toast("手机号没有填写");
                    return false;
                }
                if(!(/^1[34578]\d{9}$/.test(params.mobile))){
                    Toast("手机号码有误");
                    return false;
                }
                if(params.name == null || params.name == ""){
                    Toast("姓名没有填写");
                    return false;
                }
                if(params.userName == null || params.userName == ""){
                    Toast("昵称没有填写");
                    return false;
                }
                if(params.position == null || params.position == ""){
                    Toast("职位没有填写");
                    return false;
                }
                if(params.password == null || params.password == ""){
                    Toast("密码没有填写");
                    return false;
                }
                return true;
            }
        }
    }
</script>
